home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_dates.zip / NDATE.C < prev    next >
Text File  |  1987-05-29  |  4KB  |  130 lines

  1. /************************************************************************/
  2. /*  (c)  1987 by James N. Seed,  Dallas, TX  -  all rights reserved.    */
  3. /*                                    */
  4. /*  This program is provided for example purposes only.  It, and/or    */
  5. /*  its executable equivalent, may not be sold or distributed in any    */
  6. /*  form for profit, without the prior written consent of the author.    */
  7. /************************************************************************/
  8.  
  9. #define LINT_ARGS                /* wholly advisable!!! */
  10.  
  11. #pragma check_stack-                /* special for MSC */
  12.  
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <c_dates.h>
  17.  
  18. static    char *errtyp1 = "\nerror:  cannot interpret the %s in the start date.\n";
  19. static    char *errtyp2 = "\nerror:  start date is not a valid date.\n";
  20.  
  21. static    char *fldnme[3] = { "month", "day", "year" };    /* this is for the */
  22.                             /* error output.   */
  23.  
  24. _setenvp()    { }                /* special for MSC */
  25.  
  26. main( argc, argv )
  27.  
  28. int      argc;
  29. char    **argv;
  30.  
  31. {
  32.     unsigned int    gregdte[3], i;        /* m, d, & y ( & index )  */
  33.     unsigned long    julian;            /* julian long start date */
  34.     int        type = 0;        /* type of days to add      */
  35.     char        dtestr[29];        /* to hold verbose dates  */
  36.     char        *nxtfld;        /* for dividing date parm */
  37.     unsigned int    chkdte[3];        /* used to check date     */
  38.  
  39.     if ( argc-- != 3 )            /* two parms input ? */
  40.     {
  41.          printf( "\nNDATE   is a program that calculates a new date that is equal" );
  42.          printf( "\n=====   to the starting date input plus or minus the number of" );
  43.          printf( "\n        days of the specified type.\n" );
  44.          printf( "\nusage:  NDATE [ sdate [+/-]days[A|S|W] ]" );
  45.          printf( "\n\nwhere:" );
  46.          printf( "\n\tsdate is the starting date, and\n" );
  47.          printf( "\n\t[+/-]days is the ( signed ) number of days to be added," );
  48.          printf( "\n\t\t  defined as ONE of the following allowable types:\n" );
  49.          printf( "\n\t\t  A - all days (default)," );
  50.          printf( "\n\t\t  S - all days excluding Sundays," );
  51.          printf( "\n\t\t  W - all days excluding weekends" );
  52.          printf( "\n\n- The starting date MUST be input in the \"month/day/year\" format.\n" );
  53.  
  54.          return( 0 );
  55.     }
  56.  
  57.     /***********************************************************/
  58.     /* First, convert the input date to the m, d, & y integers */
  59.     /***********************************************************/
  60.  
  61.     for ( i = 0; i < 2; ++i )            /* pick off m & d  */
  62.     {
  63.           if ( gregdte[i] = atoi( argv[1] ) )
  64.           {
  65.            if ( nxtfld = strstr( argv[1], "/" ) )
  66.            {
  67.             argv[1] = ++nxtfld;
  68.            }
  69.            else
  70.            {
  71.             printf( errtyp1, fldnme[++i] );
  72.             return( 1 );
  73.            }
  74.           }
  75.           else
  76.           {
  77.            printf( errtyp1, fldnme[i] );
  78.            return( 1 );
  79.           }
  80.     }
  81.  
  82.     gregdte[i] = atoi( argv[1] );            /* then pick off y */
  83.  
  84.     /***********************************************************/
  85.     /*   this line converts the m, d, & y to the JULIAN date   */
  86.     /***********************************************************/
  87.  
  88.     julian = gtoj( gregdte[0], gregdte[1], gregdte[2] );
  89.  
  90.     /*****************************************************/
  91.     /*   these lines check to see if the date is valid   */
  92.     /*****************************************************/
  93.  
  94.     jtog( julian, (char *)chkdte, 0 );
  95.  
  96.     for ( i = 0; i < 3; ++i )
  97.     {
  98.           if ( chkdte[i] != gregdte[i] )
  99.           {
  100.            printf( errtyp2 );
  101.            return( 1 );
  102.           }
  103.     }
  104.  
  105.     fulldte( julian, dtestr );    /* save verbose to print out later */
  106.  
  107.     /***********************************************/
  108.     /*   these lines compute the new julian date   */
  109.     /***********************************************/
  110.  
  111.     if ( strchr( argv[2], 'S' ) ) type = 1;
  112.     if ( strchr( argv[2], 'W' ) ) type = 2;
  113.  
  114.     julian = newdate( julian, atol( argv[2] ), type );
  115.  
  116.     /******************************************/
  117.     /*   these lines print out the new date   */
  118.     /******************************************/
  119.  
  120.     printf( "\n\t  %li days from %s%s is", atol( argv[2] ), dtestr,
  121.          type ? type == 1 ? ", excluding Sundays,"
  122.                   : ", excluding weekends," : "" );
  123.  
  124.     fulldte( julian, dtestr );
  125.  
  126.     printf( "\n\n\t  %s.\n", dtestr );
  127.  
  128.     return( 0 );
  129. }
  130.